﻿using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace NetTao.Config
{
    public class ContentManage
    {
        private static ContentManage _contentManage = null;

        public static ContentManage Instace()
        {
            return _contentManage ?? (_contentManage = new ContentManage());
        }

        private ContentInfo _content = null;

        public ContentInfo Content
        {
            get { return _content; }
        }

        private static readonly object ObjLock = new object();

        /// <summary>
        /// 初始化配置文件
        /// </summary>
        public void Load()
        {
            lock (ObjLock)
            {
                _content = new ContentInfo();
                using (FileStream stream = File.Open(@"conf\content.xml", FileMode.Open))
                {
                    var xmlserializer = new XmlSerializer(typeof (ContentInfo));
                    _content = xmlserializer.Deserialize(stream) as ContentInfo;
                    stream.Close();
                    stream.Dispose();
                }
            }
        }

        private Dictionary<string, string> _lagerSortNames = new Dictionary<string, string>();

        private Dictionary<string, int> _lagerSortIDs = new Dictionary<string, int>();

        private Dictionary<string, string> _smallSortNames = new Dictionary<string, string>();

        private Dictionary<string, int> _smallSortIDs = new Dictionary<string, int>();

        public string GetLagerSortName(string sortName)
        {
            if (_lagerSortNames == null || _lagerSortNames.Count == 0)
            {
                LoadSubSort(_content.LagerSortCorresponding, ref _lagerSortNames, ref _lagerSortIDs);
            }
            if (_lagerSortNames.ContainsKey(sortName))
            {
                return _lagerSortNames[sortName];
            }
            else
            {
                return _content.DefaultLagerSort;
            }
        }

        public int GetLagerSortID(string sortName)
        {
            if (_lagerSortNames == null || _lagerSortNames.Count == 0)
            {
                LoadSubSort(_content.LagerSortCorresponding, ref _lagerSortNames, ref _lagerSortIDs);
            }
            return _smallSortIDs.ContainsKey(sortName) ? _smallSortIDs[sortName] : _content.DefaultLagerSortID;
        }

        public string GetSmallSortName(string sortName)
        {
            if (_smallSortNames == null || _smallSortNames.Count == 0)
            {
                LoadSubSort(_content.SmallSortCorresponding, ref _smallSortNames, ref _smallSortIDs);
            }
            if (_smallSortNames.ContainsKey(sortName))
            {
                return _smallSortNames[sortName];
            }
            else
            {
                return _content.DefaultSmallSort;
            }
        }

        public int GetSmallSortID(string sortName)
        {
            if (_smallSortNames == null || _smallSortNames.Count == 0)
            {
                LoadSubSort(_content.SmallSortCorresponding, ref _smallSortNames, ref _smallSortIDs);
            }
            if (_smallSortIDs.ContainsKey(sortName))
            {
                return _smallSortIDs[sortName];
            }
            else
            {
                return _content.DefaultSmallSortID;
            }
        }

        private void LoadSubSort(string sortString, ref Dictionary<string, string> sortNames,
                                 ref Dictionary<string, int> sortIDs)
        {
            string[] arrLagerSorts = Content.LagerSortCorresponding.Split('♂');
            foreach (var arrSort in arrLagerSorts)
            {
                string[] arrID = arrSort.Split('|');
                if (arrID.Length > 1)
                {
                    int lagerID = int.Parse(arrID[0]);
                    string[] arrName = arrID[1].Split('=');
                    if (arrName.Length > 0)
                    {
                        string lagerName = arrName[0];
                        string[] arrNames = arrName[1].Split(',');
                        foreach (var name in arrNames)
                        {
                            if (string.IsNullOrEmpty(name)) continue;
                            sortNames.Add(name, lagerName);
                        }
                        sortIDs.Add(lagerName, lagerID);
                    }
                }
            }
        }

        private readonly Dictionary<string, string> _smartReplaceString = new Dictionary<string, string>();

        private void LoadSmartReplaceList()
        {
            var arrs = Config.ContentManage.Instace().Content.SmartMatchString.Split('|');
            foreach (var arr in arrs)
            {
                var strs = arr.Split('♂');
                if (strs.Length > 1)
                {
                    _smartReplaceString[strs[0]] = strs[1];
                }
            }
        }

        public Dictionary<string,string> GetSmartReplaceString()
        {
            if (_smartReplaceString == null || _smartReplaceString.Count == 0)
            {
                LoadSmartReplaceList();
            }
            return _smartReplaceString;
        }
    }
}